<!-- HTML for static distribution bundle build -->
<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Swagger UI</title>
    <link
      rel="stylesheet"
      type="text/css"
      href="https://unpkg.com/swagger-ui-dist@4.3.0/swagger-ui.css"
      integrity="sha384-pzdBB6iZwPIzBHgXle+9cgvKuMgtWNrBopXkjrWnKCi3m4uJsPPdLQ4IPMqRDirS"
      crossorigin="anonymous"
      referrerpolicy="no-referrer"
    />
    <link rel="icon" type="image/png" href="favicon-32x32.png" sizes="32x32" />
    <link rel="icon" type="image/png" href="favicon-16x16.png" sizes="16x16" />
    <style>
      html {
        box-sizing: border-box;
        overflow: -moz-scrollbars-vertical;
        overflow-y: scroll;
      }

      *,
      *:before,
      *:after {
        box-sizing: inherit;
      }

      body {
        margin: 0;
        background: #fafafa;
      }

      .topbar-wrapper img {
        content: url('public/img/grafana_icon.svg');
      }
    </style>
  </head>

  <body>
    <div id="swagger-ui"></div>

    <script
      nonce="[[.Nonce]]"
      src="https://unpkg.com/swagger-ui-dist@4.3.0/swagger-ui-bundle.js"
      charset="UTF-8"
      integrity="sha384-BGJ5JzR5LEl4ETmxXXlZtXtMWj3uQ9jj9/OHe3yrn5rrtAyLOz1SyyzwMfuwZgPc"
      crossorigin="anonymous"
      referrerpolicy="no-referrer"
    ></script>
    <script
      nonce="[[.Nonce]]"
      src="https://unpkg.com/swagger-ui-dist@4.3.0/swagger-ui-standalone-preset.js"
      charset="UTF-8"
      integrity="sha384-AWSfISmlS8fS336GXRkpL0Uv6EbCpsFfXDUwmklhbb3SctGSuvXWBcbjERjgf/e4"
      crossorigin="anonymous"
      referrerpolicy="no-referrer"
    ></script>
    <script nonce="[[.Nonce]]">
      window.onload = async function () {
        // the trailing slash breaks relative URL loading
        if (window.location.pathname.endsWith('/')) {
          const idx = window.location.href.lastIndexOf('/');
          window.location.href = window.location.href.substring(0, idx);
          return;
        }

        const urlParams = new URLSearchParams(window.location.search);
        const v2 = { name: 'Grafana API (OpenAPI v2)', url: 'public/api-merged.json' };
        const v3 = { name: 'Grafana API (OpenAPI v3)', url: 'public/openapi3.json' };
        const urls = urlParams.get('show') == 'v3' ? [v3, v2] : [v2, v3];
        try {
          const rsp = await fetch('openapi/v3');
          const apis = await rsp.json();
          for (const [key, value] of Object.entries(apis.paths)) {
            const parts = key.split('/');
            if (parts.length == 3) {
              urls.push({
                name: `${parts[1]}/${parts[2]}`,
                url: value.serverRelativeURL.substring(1), // remove initial slash
              });
            }
          }
          urls.push({ name: 'Grafana apps (OpenAPI v2)', url: 'openapi/v2' });
        } catch (err) {
          // console.warn('Error loading k8s apis', err);
        }

        // Begin Swagger UI call region
        const ui = SwaggerUIBundle({
          urls,
          dom_id: '#swagger-ui',
          deepLinking: true,
          presets: [SwaggerUIBundle.presets.apis, SwaggerUIStandalonePreset],
          plugins: [SwaggerUIBundle.plugins.DownloadUrl],
          layout: 'StandaloneLayout',
          filter: true,
          tagsSorter: 'alpha',
          tryItOutEnabled: true,
        });

        window.ui = ui;
      };
    </script>
  </body>
</html>
